package com.demo.travel.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.demo.travel.R;
import com.demo.travel.activity.EventDetailActivity;
import com.demo.travel.adapter.EventAdapter;
import com.demo.travel.base.BaseFragment;
import com.demo.travel.bean.ItemBean;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 *
 * Create time 2021/6/15 9:40
 * 活动
 */
public class EventFragment extends BaseFragment {


    @BindView(R.id.mBanner)
    Banner mBanner;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private EventAdapter eventAdapter;

    @Override
    public int getLayout() {
        return R.layout.fragment_event;
    }

    @Override
    public void initData() {
        initBanner();
        eventAdapter = new EventAdapter();
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview.setAdapter(eventAdapter);

        eventAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ItemBean itemBean = eventAdapter.getData().get(position);
                Intent intent = new Intent(getActivity(), EventDetailActivity.class);
                intent.putExtra("itemBean", itemBean);
                getActivity().startActivity(intent);
            }
        });

        ItemBean itemBean1 = new ItemBean(getStringText(R.string.hd1_title), R.mipmap.hd1);
        itemBean1.setContent(getStringText(R.string.hd1_content));
        ItemBean itemBean2 = new ItemBean(getStringText(R.string.hd2_title), R.mipmap.hd2);
        itemBean2.setContent(getStringText(R.string.hd2_content));
        ItemBean itemBean3 = new ItemBean(getStringText(R.string.hd3_title), R.mipmap.hd3);
        itemBean3.setContent(getStringText(R.string.hd3_content));
        eventAdapter.addData(itemBean1);
        eventAdapter.addData(itemBean2);
        eventAdapter.addData(itemBean3);
    }

    @Override
    public String getTitle() {
        return "活动";
    }

    @Override
    public void requestData() {

    }

    private void initBanner() {
        int[] imageResourceID = new int[]{R.mipmap.hd1, R.mipmap.hd2, R.mipmap.hd3};
        List<Integer> imgeList = new ArrayList<>();
        //轮播标题
        String[] mtitle = new String[]{getStringText(R.string.hd1_title), getStringText(R.string.hd2_title), getStringText(R.string.hd3_title)};
        List<String> titleList = new ArrayList<>();

        for (int i = 0; i < imageResourceID.length; i++) {
            imgeList.add(imageResourceID[i]);//把图片资源循环放入list里面
            titleList.add(mtitle[i]);//把标题循环设置进列表里面
            //设置图片加载器，通过Glide加载图片
            mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(getActivity()).load(path).into(imageView);
                }
            });
            //设置轮播的动画效果,里面有很多种特效,可以到GitHub上查看文档。
            mBanner.setBannerAnimation(Transformer.Accordion);
            mBanner.setImages(imgeList);//设置图片资源
            mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//设置banner显示样式（带标题的样式）
            mBanner.setBannerTitles(titleList); //设置标题集合（当banner样式有显示title时）
            //设置指示器位置（即图片下面的那个小圆点）
            mBanner.setIndicatorGravity(BannerConfig.CENTER);
            mBanner.setDelayTime(3000);//设置轮播时间3秒切换下一图
//            mBanner.setOnBannerListener(this);//设置监听
            mBanner.start();//开始进行banner渲染
        }
    }
}
